//
//  LeadMainViewController.m
//  SmileRead
//
//  Created by dllo on 15/9/1.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "LeadMainViewController.h"
#import "Path.h"
#import "MainViewController.h"
#import "UnderViewController.h"
#import "LeftSlideViewController.h"
#import "AppDelegate.h"

@interface LeadMainViewController ()<UIScrollViewDelegate>

{
    NSInteger _count;
}

@property (nonatomic, retain) UIScrollView *scroll;

// 装图片
@property (nonatomic, retain) NSMutableArray *array;

@property (nonatomic, retain) UIVisualEffectView *effectView;

@property (nonatomic, retain) UIButton *button;

@property (nonatomic, retain) UILabel *contentLabel;

// 引导语
@property (nonatomic, retain) NSMutableArray *arrayOflead;

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) UIImageView *imageView;

@end

@implementation LeadMainViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = @[@"noted.jpg", @"7.jpg", @"8.jpg", @"9.jpg", @"noted.jpg", @"7.jpg"].mutableCopy;
        
        // 引导语
        self.arrayOflead = @[@"乐: 汇集各种搞笑笑话", @"听: 让动听的声音梳理你得心情", @"阅: 用优美的文章浇灌你的心灵", @"七天日记: 记录你一个星期的点点滴滴", @""].mutableCopy;
    }
    return self;
}

- (void)dealloc
{
    [_imageView release];
    [_titleLabel release];
    [_contentLabel release];
    [_button release];
    [_effectView release];
    [_scroll release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSubView];
}

- (void)createSubView
{
    UIImageView *backimage = [[UIImageView alloc] initWithFrame:CGRectMake(0 * widthFit, 0 * heightFit, self.view.frame.size.width, self.view.frame.size.height)];
    backimage.userInteractionEnabled = YES;
    backimage.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"lead.jpg"]];
    [self.view addSubview:backimage];
    [backimage release];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [backimage addSubview:self.effectView];
    [_effectView release];
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2 - 250 / 2 * heightFit, self.view.frame.size.width, 250 * heightFit)];
    self.scroll.backgroundColor = [UIColor cyanColor];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * self.array.count, 0);
    self.scroll.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    self.scroll.delegate = self;
    self.scroll.pagingEnabled = YES;
    [self.view addSubview:self.scroll];
    [_scroll release];
    
    // 铺图片
    for (int i = 0; i < self.array.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        imageView.userInteractionEnabled = YES;
        NSString *path = [Path pathOfImageWithName:self.array[i]];
        imageView.image = [UIImage imageWithContentsOfFile:path];
        [self.scroll addSubview:imageView];
        [imageView release];
    }
    
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * widthFit, 20 * heightFit, self.view.frame.size.width - 20 * widthFit, 100 * heightFit)];
    //self.titleLabel.backgroundColor = [UIColor redColor];
    self.titleLabel.font = [UIFont systemFontOfSize:25];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = [UIColor whiteColor];
    NSAttributedString *string1 = [[NSAttributedString alloc] initWithString:@"Our mind is a sponge, Our heart is a stream." attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.titleLabel.attributedText = string1;
    [self.effectView.contentView addSubview:self.titleLabel];
    [_titleLabel release];
    
    // 为titleLabel添加动画
    
    
    
    
    
    
    
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * widthFit, 120 * heightFit, self.view.frame.size.width - 40 * widthFit, self.view.frame.size.height / 2 - self.scroll.frame.size.height / 2 - 120 * heightFit)];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = [UIColor blueColor];
    self.contentLabel.font = [UIFont systemFontOfSize:25];
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.arrayOflead[0] attributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}];
    self.contentLabel.attributedText = string;
    //self.contentLabel.backgroundColor = [UIColor cyanColor];
    [self.effectView.contentView addSubview:self.contentLabel];
    [_contentLabel release];
    
    // 设置向右滑动
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50 * widthFit, self.view.frame.size.height / 2 + self.view.frame.size.height / 2 - self.scroll.frame.size.height / 2 - 120 * heightFit + 80 * heightFit, 100 * widthFit, 45 * heightFit)];
    self.imageView.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"huadong.png"]];
    [self.view addSubview:self.imageView];
    [_imageView release];
    // 为滑动添加动画
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    // 无限循环
    [animation setRepeatCount:INFINITY];
    // 循环时间
    [animation setDuration:1];
    // 自动执行
    [animation setAutoreverses:YES];
    NSNumber *from = [NSNumber numberWithFloat:1.0];
    animation.fromValue = from;
    NSNumber *to = [NSNumber numberWithFloat:0.2];
    animation.toValue = to;
    [self.imageView.layer addAnimation:animation forKey:@"a"];
    
}

// 进入app主界面
- (void)button:(UIButton *)button
{
    // 进入app主界面
    MainViewController *main = [[MainViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:main];
    // self.window.rootViewController = navi;
    
    UnderViewController *under = [[UnderViewController alloc] init];
    
    // 创建代理并指定代理人
    LeftSlideViewController *drawer = [[LeftSlideViewController alloc] initWithLeftView:under andMainView:navi delegate:main];
    [under receiveWithDrawer:drawer];
    //将抽屉指定window的根视图控制器
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    tempAppDelegate.window.rootViewController = drawer;
    [under release];
    [drawer release];
    [main release];
    [navi release];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake((self.array.count - 2) * self.view.frame.size.width, 0);
    }
    if (scrollView.contentOffset.x == (self.array.count - 1) * self.view.frame.size.width) {
        if (_count == 0) {
            
            [self.imageView removeFromSuperview];
            self.button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 80 * widthFit, self.view.frame.size.height / 2 + self.scroll.frame.size.height / 2 + 60 * heightFit, 160 * widthFit, 40 * heightFit)];
            [self.button setTitle:@"点击进入" forState:UIControlStateNormal];
            [self.button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
            self.button.layer.cornerRadius = 10;
            self.button.backgroundColor = [UIColor greenColor];
            [self.view addSubview:self.button];
            [_button release];
        }
        _count++;
        scrollView.contentOffset = CGPointMake( self.view.frame.size.width, 0);
    }
    
    NSInteger index = scrollView.contentOffset.x / self.scroll.frame.size.width - 1;
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:self.arrayOflead[index] attributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}];
    self.contentLabel.attributedText = string;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
